<template>
  <div>
    <el-dialog
      :title="ruleForm.id?'编辑客户':'添加客户'"
      width="60%"
      :visible="visagents"
      @close="cancelBtn"
    >
      <div class="content">
        <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="100px" class="demo-ruleForm">

          <el-form-item label="经纪">
            <!-- 下拉菜单 -->
            <el-select v-model="ruleForm.agent" placeholder="请选择经纪人">
              <el-option
                v-for="item in Agentslist"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="ruleForm.email" />
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="ruleForm.phone" />
          </el-form-item>
          <el-form-item label="微信">
            <el-input v-model="ruleForm.wechat" />
          </el-form-item>
          <el-form-item label="描述" prop="desc">
            <el-input v-model="ruleForm.desc" type="textarea" />
          </el-form-item>
          <el-form-item label="封面">
            <Upload ref="upload" />
          </el-form-item>
        </el-form>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmBtn">确 定</el-button>
        <el-button @click="cancelBtn">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// 获取经纪人列表
import { getAgentslist } from '@/api/agents'
// 获取客户
import { getclientdetails, putclients, postclients } from '@/api/clients'
export default {
  props: {
    visagents: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单数据
      ruleForm: {
        agent: '', // 经纪的id
        name: '', // 名字
        desc: '', // 描述
        phone: '', // 电话
        email: '', // 邮箱
        wechat: '', // 微信
        image: '' // 封面

      },
      Agentslist: [],
      clientsid: 0,
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }

        ],
        desc: [
          { required: true, message: '请输入描述', trigger: 'blur' }

        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' }

        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }

        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }

        ]
      }
    }
  },
  async created() {
    // 获取经纪人列表
    const res = await getAgentslist({ _limit: 100 })
    console.log(98, res)
    this.Agentslist = res.data
  },
  methods: {
    // 确定按钮
    confirmBtn() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            // 编辑
            await putclients({ ...this.ruleForm, image: this.$refs.upload.imageUrl })
            //   提示
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
          } else {
            await postclients({ ...this.ruleForm, image: this.$refs.upload.imageUrl })
            //   提示
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          }
          //  关闭弹窗
          this.cancelBtn()
          //   渲染
          this.$emit('getlist')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 编辑客户数据回填
    async getclients(id) {
      // 保存当前点击的id
      this.clientsid = id
      const details = await getclientdetails(id)
      console.log(2222, details)
      this.ruleForm = details.data
      //  获取上传图片组件实例，实现图片显示
      this.$refs.upload.imageUrl = details.data.image
      console.log(8888, this.$refs.upload.imageUrl)
    },
    // 取消按钮
    cancelBtn() {
      this.ruleForm = {
        name: '', // 名字
        desc: '', // 描述
        phone: '', // 电话
        address: '', // 地址
        email: '', // 邮箱
        image: '' // 封面

      }
      this.$refs.upload.imageUrl = ''
      this.$refs.ruleForm.resetFields()
      this.$emit('update:visagents', false)
    }
  }
}
</script>
      <style>
   .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .content{
      height: 400px;
      overflow: auto;
    }
      </style>

